package com.goldgov.information.service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import com.goldgov.information.service.impl.ArticleRulesServiceImpl;
import com.goldgov.utils.BeanUtil;
import com.goldgov.utils.CrawlerUtils;

@Component
@EnableScheduling
public class CrawlerCollectSchedule {

	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@Lazy(false)
	@Scheduled(cron = "${task.cron.autoCrawler}")
	public void collectPolling() {
		  //获取当前时间
		ArticleRules a;
		ArticleRulesQuery query=new ArticleRulesQuery();
        LocalDateTime localDateTime =LocalDateTime.now();
        query.setSearchRunningState(ArticleRules.RUNNING_STATE_YES);
        query.setPageSize(-1);
        List<ArticleRules> artileRulesList=BeanUtil.getBean(ArticleRulesServiceImpl.class).getArticleRulesList(query);
        logger.info("当前时间为:" + localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        logger.info("===========>待爬取任务数："+artileRulesList.size());
        for(ArticleRules ar:artileRulesList) {
        	new CrawlerUtils(ar.getRulesID());
			//更新最后运行时间
			a = new ArticleRules();
			a.setRulesID(ar.getRulesID());
			// 更新规则操作时间
			a.setLastOperationDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
	        BeanUtil.getBean(ArticleRulesServiceImpl.class).updateArticleRules(a);
        }
	}
}
